package edu.ntu.cloudroid.server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.google.appengine.api.datastore.Key;

import edu.ntu.cloudroid.model.UserData;
import edu.ntu.cloudroid.model.data.LocationInfo;
import edu.ntu.cloudroid.model.data.TraceData;
import edu.ntu.cloudroid.model.data.VitalSignData;
import edu.ntu.cloudroid.model.profile.ProfileData;
import edu.ntu.cloudroid.nongui.com.LogRequest;
import edu.ntu.cloudroid.nongui.com.VitalRequestWithFilteringFunction;
import edu.ntu.cloudroid.nongui.com.MetaDataRequest;
import edu.ntu.cloudroid.nongui.com.RequestNoInputs;
import edu.ntu.cloudroid.nongui.com.AbstractResponse;
import edu.ntu.cloudroid.server.backendservice.DataCache;
import edu.ntu.cloudroid.server.backendservice.FriendService;
import edu.ntu.cloudroid.server.backendservice.PolicyService;
import edu.ntu.cloudroid.server.backendservice.ProfileService;

public class RequestServlet extends HttpServlet{
	private static final long serialVersionUID = 1L;

	public static final String METHOD_NAME = "requestServlet";

	public static Logger logger = Logger.getLogger(RequestServlet.class
			.getName());

	@Override
	@SuppressWarnings("unchecked")
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		ObjectInputStream ois = new ObjectInputStream(req.getInputStream());
		
		try {
			RequestNoInputs request = (RequestNoInputs) ois.readObject();			
			ois.close();			
			
			if (request instanceof LogRequest){
				LogRequest lr = (LogRequest)request;
				AbstractResponse ar = new AbstractResponse();
				if (lr.getCurrentOffset().equals("purge_cache")){
					RequestServlet.logger.info("purging cache");
					DataCache.purgeCache();
					FriendService.purgeCache();
					PolicyService.purgeCache();
					ProfileService.purgeCache();
				}
				else{
					ar= BackendServiceManager.getLogOffset(lr);
				}
				ObjectOutputStream oos = new ObjectOutputStream(
						resp.getOutputStream());
				oos.writeObject(ar);
			}
			if (request instanceof MetaDataRequest){
				MetaDataRequest mdr = (MetaDataRequest)request;
				UserData ud=null; 
				switch (mdr.getRequestType()){
				case MetaDataRequest.Profile:
					ud = BackendServiceManager.getProfileMetaData(mdr.getUserName());
					break;
				case MetaDataRequest.Friend:
					ud = BackendServiceManager.getFriendMetaData(mdr.getUserName());
					break;
				case MetaDataRequest.Policy:
					ud = BackendServiceManager.getDefaultPolicyMetaData(mdr.getUserName());
					break;
				case MetaDataRequest.Group:
					ud = BackendServiceManager.getGroupMetaData(
							mdr.getUserName(), mdr.getGroupName());
					break;
				}
				ObjectOutputStream oos = new ObjectOutputStream(
						resp.getOutputStream());
				oos.writeObject(ud);
			} else if (request instanceof VitalRequestWithFilteringFunction){
				AbstractResponse response = BackendServiceManager
						.getDataForDirectRequest((VitalRequestWithFilteringFunction) request);
				ObjectOutputStream oos = new ObjectOutputStream(
						resp.getOutputStream());
				oos.writeObject(response);
			}
			else
			{
				AbstractResponse response;				
				if (request.isForAllFriendsRequest())
					response = BackendServiceManager
							.getDataFromAllFriends(request);
				else
					response = BackendServiceManager
							.getDataFromAFriend(request);
				ObjectOutputStream oos = new ObjectOutputStream(
						resp.getOutputStream());
				oos.writeObject(response);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
	}

}
